﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sklad1_2.Add.Update;
using Sklad1_2.Data;
using Sklad1_2.Default;

namespace Sklad1_2.Search
{
    public partial class SearchZakaz : Defaultform
    {
        private string query=Helper.QueryMain,q,q1;
        private string summa = "select sum(summa) from main a where a.id=a.id";
        private string ostatok = "select sum(ostatok) from main a where a.id=a.id";

        public SearchZakaz()
        {
            InitializeComponent();
            Init();
            //query = Helper.QueryMain;
        }

        public override sealed void Init()
        {
            ABase.AddItem(comboBox1, "customer");
            ABase.AddItemProduct(comboBox2, "product");
            ABase.AddItemDBGridView(dataGridView1, Helper.QueryMain);
            ABase.AddItemDBGridView(dataGridView2,Helper.QueryRas+" and b.id=1");
            Helper.Hidden(dataGridView1, 0, 1, 2, 3, 4, 5,12);
            toolStripStatusLabel1.Text = "Общяя сумма=" + ABase.SelectFieldQ(summa);
            
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                comboBox1.Enabled = true;
            }
            else
            {
                comboBox1.Enabled = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                comboBox2.Enabled = true;
            }
            else
            {
                comboBox2.Enabled = false;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;
            }
            else
            {
                dateTimePicker1.Enabled = false;
                dateTimePicker2.Enabled = false;
            }
        }
        void ras()
        {
            
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            query = Helper.QueryMain;
            q = summa;
            q1 = ostatok;
            if (checkBox1.Checked)
            {
                query += " and f.customer_id=" + Convert.ToString(comboBox1.SelectedValue); 
                q += " and a.customer_id=" + Convert.ToString(comboBox1.SelectedValue);
                q1 += " and a.customer_id=" + Convert.ToString(comboBox1.SelectedValue);
            }
            if (checkBox2.Checked)
            {
                query +=" and f.product_id=" + Convert.ToString(comboBox2.SelectedValue);
                q += " and a.product_id=" + Convert.ToString(comboBox2.SelectedValue);
                q1 += " and a.product_id=" + Convert.ToString(comboBox2.SelectedValue);
            }
            if (checkBox3.Checked)
            {
                query +=" and f.datec between cdate('" + dateTimePicker1.Text + "') and cdate('" + dateTimePicker2.Text + "')";;
                q += " and a.datec between cdate('" + dateTimePicker1.Text + "') and cdate('" + dateTimePicker2.Text + "')";
                q1 += " and a.datec between cdate('" + dateTimePicker1.Text + "') and cdate('" + dateTimePicker2.Text + "')";

            }
            ABase.AddItemDBGridView(dataGridView1, query);
            toolStripStatusLabel1.Text = "Общяя сумма=" + ABase.SelectFieldQ(q) + " cом";
            toolStripStatusLabel2.Text = "Не выплаченно= " + ABase.SelectFieldQ(q1) + " cом";
            toolStripStatusLabel3.Visible = false;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex==-1)
            {
                return;
            }
            data=new List<string>();

            Helper.Db2List(dataGridView1, e, ref data, 0, 1, 2, 3, 4, 5, 11, 12, 13, 14, 15, 16, 17);
            UpdateMain aUpdateMain = new UpdateMain(data);
            aUpdateMain.FormClosed += Bind;
            aUpdateMain.ShowDialog();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            if (e.RowIndex != -1)
            {
                string quer = Helper.QueryRas + " and a.main_id=" + Convert.ToString(dataGridView1[0, e.RowIndex].Value);
                ABase.AddItemDBGridView(dataGridView2, quer);
                toolStripStatusLabel1.Text = "Общяя сумма=" + Convert.ToString(dataGridView1[14, e.RowIndex].Value) + " cом";
                toolStripStatusLabel3.Text = "Остаток=" + Convert.ToString(dataGridView1[17, e.RowIndex].Value) + "cом";
            }
        }
    }
}
